public class Solution {
    public int firstMissingPositive(int[] nums) {
        int n = nums.length;

        for(int i = 0; i < n; i++){
            while(nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] != nums[i]  ){
                int tmp = nums[nums[i] - 1];
                nums[nums[i] - 1] = nums[i];
                nums[i] = tmp;

            }
        }

        for(int j = 0; j < n; j++){
            if(nums[j] != j+1){
                return j+1;
            }
        }
        return n+1;

    }
}
